home *** CD-ROM | disk | FTP | other *** search
- /*
- // httplog.c
- //
- // (c) Armin Obersteiner
- //
- // Parsing ncsa(cern) httpd logfile
- //
- // USAGE: httplog [-s <keyword>] [-l] <logfile>
- //
- // <logfile> - httpd <logfile>
- // -l - long: country statistics
- // -lt - long: time statistics
- // -s <keyword> - search for <keyword>
- //
- // compiles on: MaxonC++ (amiga)
- // gcc (amiga)
- // gcc (bsd)
- // (it should actually compile on any platform then :)
- //
- // it´s made to work on logfiles with american date format and austrian/german time format (24h)
- //
- // to adapt for other logfiles:
- // the first entry should be the site
- // the second entry is the date in brackets [ ]
- // to adapt this look in lines after "fopen"
- // to include new countries add lines to structure "struct dummy c"
- // don´t forget to increase "country_anz"
- // to adapt time and date do the same with "struct dummy d" / "struct dummy t"
- // don´t forget to increase/decrease "day_anz" :) / "time_anz"
- // the second entry in these stuctures (dummy) is the string to search for
- // (use spaces or brackets, because it´s more reliable then)
- // the third entry is the string for output
- //
- // CU Armin :)
- //
- // ( Armin.Obersteiner@giga.or.at )
- */
-
-
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <math.h>
-
- FILE *f;
-
- struct dummy
- {
- int i;
- char *ext;
- char *name;
- };
-
- int country_anz=10;
- struct dummy c[10]=
- {
- { 0,".at ", "austria " },
- { 0,".de ", "germany " },
- { 0,".fi ", "finnland " },
- { 0,".se ", "sweden " },
- { 0,".no ", "norway " },
- { 0,".edu ", "usa education " },
- { 0,".com ", "usa commercial " },
- { 0,".net ", "usa network " },
- { 0,".gov ", "usa goverment " },
- { 0,".jp ", "japan " }
- };
-
- int day_anz=7;
- struct dummy d[7]=
- {
- { 0,"Mon ", "mon" },
- { 0,"Tue ", "tue" },
- { 0,"Wed ", "wed" },
- { 0,"Thu ", "thu" },
- { 0,"Fri ", "fri" },
- { 0,"Sat ", "sat" },
- { 0,"Sun ", "sun" },
- };
-
- int time_anz=24;
- struct dummy t[24]=
- {
- { 0," 00:", "00" },
- { 0," 01:", "01" },
- { 0," 02:", "02" },
- { 0," 03:", "03" },
- { 0," 04:", "04" },
- { 0," 05:", "05" },
- { 0," 06:", "06" },
- { 0," 07:", "07" },
- { 0," 08:", "08" },
- { 0," 09:", "09" },
- { 0," 10:", "10" },
- { 0," 11:", "11" },
- { 0," 12:", "12" },
- { 0," 13:", "13" },
- { 0," 14:", "14" },
- { 0," 15:", "15" },
- { 0," 16:", "16" },
- { 0," 17:", "17" },
- { 0," 18:", "18" },
- { 0," 19:", "19" },
- { 0," 20:", "20" },
- { 0," 21:", "21" },
- { 0," 22:", "22" },
- { 0," 23:", "23" },
- };
-
- char prg[256];
-
- void ende(int n,char *end);
-
- void main(int argc, char *argv[])
- {
- int anz=0,i=0,j=0,count=0,x;
- double sonst=0;
- int minus_l=0;
- int minus_lt=0;
- int minus_s=0;
-
- double prozent;
-
- char file[256];
- char such[256];
-
- char line[1024],site[1024],back[1024],date[1024];
- /* char method[1024],file[1024],protocol[1024]; */
-
- if(argc<2)
- {
- printf("USAGE: %s [-s <keyword>] [-l[t]] <logfile> \n",argv[0]);
- }
- else
- {
- strcpy(prg,argv[0]);
-
- if(!strcmp(argv[1],"-s"))
- {
- if(argc==4)
- {
- strcpy(such,argv[2]);
- if(!strcmp(argv[2],"-l") || !strcmp(argv[2],"-lt")) ende(1,"wrong arguments");
- if(!strcmp(argv[3],"-l") || !strcmp(argv[3],"-lt")) ende(1,"wrong arguments");
- strcpy(file,argv[3]);
- minus_s=1;
- }
- else if(argc==5)
- {
- strcpy(such,argv[2]);
- if(!strcmp(argv[2],"-l") || !strcmp(argv[2],"-lt")) ende(1,"wrong arguments");
- if(strcmp(argv[3],"-l") && strcmp(argv[3],"-lt")) ende(1,"wrong arguments");
- strcpy(file,argv[4]);
- minus_s=1;
- if(!strcmp(argv[3],"-l"))
- {
- minus_l=1;
- }
- else minus_lt=1;
-
- }
- else ende(1,"wrong arguments");
- }
- else if( !strcmp(argv[1],"-l") )
- {
- if(argc!=3) ende(1,"wrong arguments");
-
- minus_l=1;
- strcpy(file,argv[2]);
- }
- else if( !strcmp(argv[1],"-lt") )
- {
- if(argc!=3) ende(1,"wrong arguments");
-
- minus_lt=1;
- strcpy(file,argv[2]);
- }
- else strcpy(file,argv[1]);
-
-
- if( (f=fopen(file,"r"))!=NULL )
- {
- char site_old[1024]="";
- char date_old[1024]="";
-
- while( ((fgets(line,1024,f))!=NULL) )
- {
- if(minus_s) strcpy(back,line);
-
- i++;
- strcpy(site,strtok(line,"["));
- strcpy(date,strtok(NULL,"]"));
- /*strcpy(method,strtok(NULL," "));
- strcpy(file,strtok(NULL," "));
- strcpy(protocol,strtok(NULL," \0\n")); */
-
- if(minus_s)
- {
- if(strstr(back,such)!=NULL)
- {
- if(minus_l) for(x=0;x<country_anz;x++) if(strstr(site,c[x].ext)!=NULL) (c[x].i)++;
- if(minus_lt)
- {
- for(x=0;x<day_anz;x++) if(strstr(date,d[x].ext)!=NULL) (d[x].i)++;
- for(x=0;x<time_anz;x++) if(strstr(date,t[x].ext)!=NULL) (t[x].i)++;
- }
- anz++;
- }
- }
- else
- {
- if(minus_l) for(x=0;x<country_anz;x++) if(strstr(site,c[x].ext)!=NULL) (c[x].i)++;
- if(minus_lt)
- {
- for(x=0;x<day_anz;x++) if(strstr(date,d[x].ext)!=NULL) (d[x].i)++;
- for(x=0;x<time_anz;x++) if(strstr(date,t[x].ext)!=NULL) (t[x].i)++;
- }
- if(strcmp(site,site_old)!=0) anz++;
- }
-
- /* if(fmod(i,100)==0)printf("*"); */
-
- if(!minus_s) strcpy(site_old,site);
- }
-
- fclose(f);
-
- if(minus_s)
- {
- printf("searching for: %s\n\n",such);
- printf("access: %d/%d %8.4f %%\n",anz,i,(double)anz*(double)100/(double)i);
- }
- else printf("access: %d/%d\n",anz,i);
- if(minus_l)
- {
- if(i==0) ende(2,"logfile wrong");
-
- printf("\n");
- for(x=0;x<country_anz;x++) if( c[x].i )
- {
- if(!minus_s)
- {
- prozent=(double)c[x].i*(double)100/(double)i;
- }
- else
- {
- prozent=(double)c[x].i*(double)100/(double)anz;
- }
-
- if(prozent>0.009) printf("%s: %6.2f %%\n",c[x].name,prozent);
- sonst=sonst+prozent;
- }
- prozent=(double)100-sonst;
-
- if(prozent>0.009) printf("\nothers : %6.2f %%\n",prozent);
- }
- if(minus_lt)
- {
- if(i==0) ende(2,"logfile wrong");
-
- printf("\n");
-
- for(x=0;x<day_anz;x++) if( d[x].i )
- {
- if(!minus_s)
- {
- prozent=(double)d[x].i*(double)100/(double)i;
- }
- else
- {
- prozent=(double)d[x].i*(double)100/(double)anz;
- }
-
- if(prozent>0.009) printf("%s: %6.2f %%\n",d[x].name,prozent);
- }
- printf("\n");
- for(x=0;x<time_anz;x++) if( t[x].i )
- {
- if(!minus_s)
- {
- prozent=(double)t[x].i*(double)100/(double)i;
- }
- else
- {
- prozent=(double)t[x].i*(double)100/(double)anz;
- }
-
- if(prozent>0.009) printf("%s: %6.2f %%\n",t[x].name,prozent);
- }
- }
- }
- else ende(5,"can´t open logfile");
-
- ende(0,"");
- }
- }
-
- void ende(int n,char *end)
- {
- if(f) fclose(f);
-
- if(strcmp(end,"")!=0) printf("%s: %s !\n",&prg,end);
- exit(n);
- }
-